<?php

include dirname(dirname(__FILE__)) . '/include/common.php';

class webServiceUpdateUser extends core {
    public function process() {
        $data = array();
        $data['user_id'] = intval($_POST['user_id']);
        $email = $this->_cleanEmail($_POST['email']);
        $user = $this->db->get_row("SELECT * FROM users WHERE `id`='" . $data['user_id'] . "' AND email='$email'");
        if (!$user) {
            $this->_JSONResponse(array('error' => 1, 'msg' => 'User does not exist'));
        }
        $data['first_name'] = strip_tags(trim($_POST['firstName']));
        $data['last_name'] = strip_tags(trim($_POST['lastName']));

        $data['fiance_first_name'] = strip_tags(trim($_POST['fianceFirstName']));
        $data['fiance_last_name'] = strip_tags(trim($_POST['fianceLastName']));
        $wedding_date_string = $_POST['weddingDate'];
        $wedding_date_int = strtotime($wedding_date_string);
        $wedding_date = date('Y-m-d', $wedding_date_int);
        if ($_POST['password'] !== "") {
            $password = md5($_POST['password']);
            $query = "UPDATE users SET first_name='".$data['first_name']."',last_name='".$data['last_name']."',
                fiance_first_name='".$data['fiance_first_name']."',fiance_last_name='".$data['fiance_last_name']."',
                    wedding_date = '$wedding_date', password='$password' WHERE email ='$email'";
        } else {
            $query = "UPDATE users SET first_name='".$data['first_name']."',last_name='".$data['last_name']."',
                fiance_first_name='".$data['fiance_first_name']."',fiance_last_name='".$data['fiance_last_name']."',
                    wedding_date = '$wedding_date' WHERE email ='$email'";
        }
        $result = $this->db->query($query);
        
        if ($result!==false) {
            $this->_JSONResponse(array('error' => 0, 'msg' => 'Your profile has been updated','firstname'=>$data['first_name'],'lastname'=>$data['last_name']));
        } else {
            $this->_JSONResponse(array('error' => 1, 'msg' => 'Oops, something went wrong. Please try again'));
        }
    }

}

$processor = new webServiceUpdateUser();
$processor->process();
?>